<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @copyright (c) 2013
 * @author Babak Taraghi 'babak.taraghi@gmail.com'
 * @license Apache License 2.0
 * @version 1.0
 * @desc Search and retrieve data
 */
class Search extends CI_Controller {



	/**
	 * Defining controller properties
	 */
	public $viewData = array();
	private $language = "english";
	private $action;



	/**
	 *
	 * Loading common required models and langs
	 * Check session to prevent illegal access
	 */
	public function __construct(){
		parent::__construct();

		$this->load->helper("common_action");
		$this->action = new Action($this);

		$this->lang->load('common', $this->language);
		$this->lang->load('search', $this->language);

		$this->load->model("tag_model","tagModel");
		$this->load->model("simple_data_model","simpleDataModel");

		$this->tagModel->init();
		$this->simpleDataModel->init("USER","UID");
	}



	/**
	 *
	 * Searching for near dishes.. mmmm
	 * @param String $type near, all, specific
	 */
	public function dishes($type="all"){

		if($this->action->userLogin()){
				
			$this->action->addJSONItem("success", 0);
			$this->action->addJSONItem("error", 0);

			$latitude = doubleval($this->input->post("latitude"));
			$longitude = doubleval($this->input->post("longitude"));
			$idDISH = doubleval($this->input->post("dish_id"));

			// check to see if the required inputs are valid
			if(($type == "near" && $latitude && $longitude && !$idDISH) || ($type == "all" && !$latitude && !$longitude && !$idDISH) || ($type == "specific" && $idDISH && !$latitude && !$longitude)){
					
				// loading data model
				$this->load->model("specific_data_model","specificDataModel");
					
				switch($type){
					case "near":
						$this->action->addJSONItem("tag", $this->tagModel->getTag("search_dishes_near"));
						// search db for near dishes
						$records = $this->specificDataModel->get10NearDishesByCoordinates($latitude, $longitude);
						break;

					case "all":
						$this->action->addJSONItem("tag", $this->tagModel->getTag("search_dishes"));
						// search db for all dishes
						$records = $this->specificDataModel->getDishes();
						break;

					case "specific":
						$this->action->addJSONItem("tag", $this->tagModel->getTag("search_dishes_specific"));
						// fetch dish from db
						$records = $this->specificDataModel->getDish($idDISH);
							
						// fetch dish comments
						$comments = $this->specificDataModel->getCommentsForDish($idDISH);
						break;
				}
					
					
				if(count($records) > 0){

					// adding full path to image fileds
					foreach($records as $record){
						if($record->DISH_IMAGE != "")
						$record->DISH_IMAGE = base_url("asset/image")."/".$record->DISH_IMAGE;
					}

					$this->action->addJSONItem("success", 1);

					if($type == "specific"){
						$this->action->addJSONItem("item",$records[0]);
						$this->action->addJSONItem("items",$comments);
					}else{
						$this->action->addJSONItem("items",$records);
					}
				}else{
					if($type == "near"){
						$this->action->addJSONItem("message", $this->lang->line("no_dishes_found"));
					}else{
						$this->action->addJSONItem("message", $this->lang->line("no_dishes_found_2"));
					}
				}
			}else{
				$this->action->addJSONItem("error", 1);
				$this->action->addJSONItem("message", $this->lang->line("error_inputs"));
			}

			// informing UI about the result
			$this->action->flushJSON();
		}
	}







	/**
	 *
	 * Searching for specific restaurant or retrieve all
	 * @param String $type simple, detail, all, name
	 */
	public function restaurants($type="all"){

		if($this->action->userLogin()){

			$this->action->addJSONItem("success", 0);
			$this->action->addJSONItem("error", 0);
			$keyword = strtolower($this->input->post("keyword"));

			// check to see if the required inputs are valid
			if(($type == "simple" && $keyword) || ($type == "detail" && $keyword) || ($type == "all" && !$keyword) || ($type == "name" && !$keyword)){
				// loading data model
				$this->load->model("specific_data_model","specificDataModel");
				if($keyword){
					// search db for keyword
					$records = $this->specificDataModel->searchForRestaurants($keyword);
				}else{
					// retrieve all records
					$records = $this->specificDataModel->searchForRestaurants();
				}
				// see if there is any item
				if(count($records) > 0){
					$items = array();
					// assigning needed fields for UI
					switch($type){
						case "simple":
							$this->action->addJSONItem("tag", $this->tagModel->getTag("search_restaurant"));
							foreach($records as $record){
								$items[] = array(
								"idRESTAURANT" 		=> $record->idRESTAURANT,
								"RESTAURANT_NAME"	=> $record->RESTAURANT_NAME,
								"DISTRICT_NAME"		=> $record->DISTRICT_NAME,
								"LATITUDE"			=> $record->LATITUDE,
								"LONGITUDE"			=> $record->LONGITUDE
								);
							}
							break;
						case "detail":
							$this->action->addJSONItem("tag", $this->tagModel->getTag("search_restaurant_detail"));
							foreach($records as $record){
								$items[] = array(
								"idRESTAURANT" 		=> $record->idRESTAURANT,
								"RESTAURANT_NAME"	=> $record->RESTAURANT_NAME,
								"DISTRICT_NAME"		=> $record->DISTRICT_NAME,
								"LATITUDE"			=> $record->LATITUDE,
								"LONGITUDE"			=> $record->LONGITUDE,
								"EMAIL"				=> $record->EMAIL,
								"WEBSITE"			=> $record->WEBSITE,
								"CUISINE_TYPE"		=> $record->NAME_TYPE,
								"GRADE"				=> $record->GRADE,
								"RATE"				=> $record->RATE,
								"ADDRESS"			=> $record->ADDRESS,
								"PHONE"				=> $record->PHONE,
								"DOE_HALAL_CERT"	=> $record->DOE_HALAL_CERT
								);
							}
							break;
						case "all":
							$this->action->addJSONItem("tag", $this->tagModel->getTag("search_restaurant"));
							foreach($records as $record){
								$items[] = array(
								"idRESTAURANT" 		=> $record->idRESTAURANT,
								"RESTAURANT_NAME"	=> $record->RESTAURANT_NAME,
								"DISTRICT_NAME"		=> $record->DISTRICT_NAME,
								"LATITUDE"			=> $record->LATITUDE,
								"LONGITUDE"			=> $record->LONGITUDE
								);
							}
							break;
						case "name":
							$this->action->addJSONItem("tag", $this->tagModel->getTag("search_restaurant_name"));
							foreach($records as $record){
								$items[] = array(
								"RESTAURANT_NAME"	=> $record->RESTAURANT_NAME
								);
							}
							break;
					}
					$this->action->addJSONItem("success", 1);
					$this->action->addJSONItem("items",$items);
				}else{
					$this->action->addJSONItem("message", $this->lang->line("no_restaurant_found"));
				}
			}else{
				$this->action->addJSONItem("error", 1);
				$this->action->addJSONItem("message", $this->lang->line("error_inputs"));
			}

			// informing UI about the result
			$this->action->flushJSON();
		}

	}

}